package com.wu.common.util;


import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

import java.lang.ref.WeakReference;

/**
 * @author wkq
 * @date 2021年08月17日 17:02
 * @des
 */


public class TimerUtil {
    //总时间
    private long totalTime = -1L;
    //间隔时间
    private long intervalTime = 0L;
    //剩余时间
    private long remainTime;
    private long systemAddTotalTime;
    private static final int TIME = 1;
    private TimerUtil.TimeListener listener;
    //当前时间
    private long curReminTime;
    //是否开启倒计时
    private boolean isStartTimer = false;
    //是否暂停
    private boolean isPause = false;
    public TimerUtil.MyHandler mHandler = new TimerUtil.MyHandler(this);

    public TimerUtil() {
    }

    public void setIntervalTime(long intervalTime) {
        this.intervalTime = intervalTime;
    }

    public void setTotalTime(long totalTime) {
        this.totalTime = totalTime;
    }

    public long getIntervalTime() {
        return this.intervalTime;
    }

    public long getTotalTime() {
        return this.totalTime;
    }

    //倒计时执行
    public void start() {
        if (this.totalTime <= 0L && this.intervalTime <= 0L) {
            throw new RuntimeException("you must set the totalTime > 0 or intervalTime >0");
        } else {
            this.systemAddTotalTime = SystemClock.elapsedRealtime() + this.totalTime;
            if (null != this.mHandler) {
                this.mHandler.sendEmptyMessage(1);
            }

        }
    }
    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            if (mHandler!=null&&listener!=null){
                if (isStartTimer) {
                   listener.onInterval(1);
                }
                mHandler.postDelayed(this,intervalTime);
            }
        }
    };
    //定时执行
    public void startTimer() {
        isStartTimer = true;
        if (null != this.mHandler) {
            this.mHandler.postDelayed(runnable,intervalTime);
        } else {
            mHandler = new TimerUtil.MyHandler(this);
            this.mHandler.postDelayed(runnable,intervalTime);
        }
    }


    public void cancel() {
        if (this.mHandler != null) {
            this.mHandler.removeMessages(1);
            if (runnable!=null){
                this.mHandler.removeCallbacks(runnable);
            }
        }

    }

    public void release() {
        if (this.mHandler != null) {
            this.mHandler.removeMessages(0);
            this.mHandler.removeCallbacks((Runnable) null);
            this.mHandler = null;
            if (runnable!=null){
                this.mHandler.removeCallbacks(runnable);
            }
        }

    }

    public void pause() {
        if (this.mHandler != null) {
            isStartTimer = false;
            this.mHandler.removeMessages(1);
            this.isPause = true;
            this.curReminTime = this.remainTime;
        }

    }

    public void resume() {
        if (this.isPause) {
            this.isPause = false;
            this.totalTime = this.curReminTime;
            this.start();
        }

    }

    private void soloveTime() {


        this.remainTime = this.systemAddTotalTime - SystemClock.elapsedRealtime();
        if (this.remainTime <= 0L) {
            if (this.listener != null) {
                this.listener.onFinish();
                this.cancel();
            }
        } else if (this.remainTime < this.intervalTime) {
            if (null != this.mHandler) {
                this.mHandler.sendEmptyMessageDelayed(1, this.remainTime);
            }
        } else {
            long curSystemTime = SystemClock.elapsedRealtime();
            if (this.listener != null) {
                this.listener.onInterval(this.remainTime);
            }

            long delay;
            for (delay = curSystemTime + this.intervalTime - SystemClock.elapsedRealtime(); delay < 0L; delay += this.intervalTime) {
            }

            if (null != this.mHandler) {
                this.mHandler.sendEmptyMessageDelayed(1, delay);
            }
        }

    }

    public void setTimerLiener(TimerUtil.TimeListener listener) {
        this.listener = listener;
    }

    public static class MyHandler extends Handler {
        WeakReference<TimerUtil> mActivity;

        MyHandler(TimerUtil activity) {
            this.mActivity = new WeakReference(activity);
        }

        public void handleMessage(Message msg) {
            TimerUtil theActivity = this.mActivity == null ? null : (TimerUtil) this.mActivity.get();
            if (theActivity != null) {
                switch (msg.what) {
                    case 1:
                        if (!theActivity.isPause) {
                            theActivity.soloveTime();
                        }
                        break;
                    case 2:
                        theActivity.isPause = true;
                }

            }
        }
    }

    public interface TimeListener {
        void onFinish();

        void onInterval(long var1);
    }

}